<template>
  <div class="app-container">
    <el-form :inline="true" :model="queryParams" label-width="150px" ref="queryForm" size="small" v-show="showSearch">
      <el-form-item label="是否为临时二维码" prop="isTemp">
        <el-select
          @keyup.enter.native="handleQuery"
          clearable
          placeholder="请输入是否为临时二维码"
          style="width: 240px"
          v-model="queryParams.isTemp">
          <el-option
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
            v-for="dict in dict.type.wx_qrcode_temp"/>
        </el-select>
      </el-form-item>
      <el-form-item label="场景值ID" prop="sceneStr">
        <el-input
          @keyup.enter.native="handleQuery"
          clearable
          placeholder="请输入场景值ID"
          v-model="queryParams.sceneStr"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery" icon="el-icon-search" size="mini" type="primary">搜索</el-button>
        <el-button @click="resetQuery" icon="el-icon-refresh" size="mini">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          @click="handleAdd"
          icon="el-icon-plus"
          plain
          size="mini"
          type="primary"
          v-hasPermi="['wx:qrCode:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          :disabled="multiple"
          @click="handleDelete"
          icon="el-icon-delete"
          plain
          size="mini"
          type="danger"
          v-hasPermi="['wx:qrCode:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          @click="handleExport"
          icon="el-icon-download"
          plain
          size="mini"
          type="warning"
          v-hasPermi="['wx:qrCode:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border :data="qrCodeList" @selection-change="handleSelectionChange" v-loading="loading">
      <el-table-column align="center" type="selection" width="55"/>
      <el-table-column align="center" label="ID" prop="id" v-if="true"/>
      <el-table-column align="center" label="类型" prop="isTemp">
        <span slot-scope="scope">{{scope.row.isTemp?'临时':'永久'}}</span>
      </el-table-column>
      <el-table-column align="center" label="场景值ID" prop="sceneStr"/>
      <el-table-column align="center" label="解析后的地址" prop="url"/>
      <el-table-column align="center" label="失效时间" prop="expireTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expireTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button
            @click="showTicket(scope.row)"
            icon="el-icon-delete"
            size="mini"
            type="text"
            v-hasPermi="['wx:qrCode:list']"
          >二维码图片
          </el-button>
          <el-button
            @click="handleDelete(scope.row)"
            icon="el-icon-delete"
            size="mini"
            type="text"
            v-hasPermi="['wx:qrCode:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
      v-show="total>0"
    />
    <el-dialog
      :visible.sync="ticketVisible"
      title="二维码图片"
      width="30%">
      <el-image
        :src="`${ticketSrc}`">
        <div slot="error" class="image-slot">
          <i class="el-icon-picture-outline"></i>
        </div>
      </el-image>
      <span class="dialog-footer" slot="footer">
        <el-button @click="ticketVisible = false" type="primary">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update :visible.sync="open" @refreshDataList="getList" ref="addOrUpdate"></add-or-update>
  </div>
</template>

<script>
  import {listQrCode, delQrCode} from "@/api/wx/qrCode";
  import AddOrUpdate from './qrcode-add-or-update';

  export default {
    name: "QrCode",
    dicts: ['wx_qrcode_temp'],
    data() {
      return {
        ticketSrc: undefined,
        ticketVisible: false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 公众号带参二维码表格数据
        qrCodeList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          appid: undefined,
          isTemp: undefined,
          sceneStr: undefined,
          ticket: undefined,
          url: undefined,
          expireTime: undefined,
        }
      };
    },
    components: {
      AddOrUpdate
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询公众号带参二维码列表 */
      getList() {
        this.loading = true;
        listQrCode(this.queryParams).then(response => {
          this.qrCodeList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd(id) {
        this.open = true;
        this.title = "添加公众号带参二维码";
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除公众号带参二维码编号为"' + ids + '"的数据项？').then(() => {
          this.loading = true;
          return delQrCode(ids);
        }).then(() => {
          this.loading = false;
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        }).finally(() => {
          this.loading = false;
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('wx/qrCode/export', {
          ...this.queryParams
        }, `qrCode_${new Date().getTime()}.xlsx`)
      },
      showTicket(row) {
        this.ticketVisible = true;
        this.ticketSrc = 'https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='+row.ticket;
      }
    }
  };
</script>
